﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Data;
using System.Collections;
using Newtonsoft.Json;
using System.Data.SqlClient;
using CMD_WEB.DAL;

namespace CMD.BAL.AUTH
{
    [DataContractAttribute]
    public class Rol
    {
        [DataMemberAttribute(EmitDefaultValue = false)]
        public Group group;
        [DataMemberAttribute(EmitDefaultValue = false)]
        public string idRol;
        [DataMemberAttribute(EmitDefaultValue = false)]
        public string dscRol;
        [DataMemberAttribute(EmitDefaultValue = false)]
        public int prio;
        [DataMemberAttribute(EmitDefaultValue = false)]
        public string rol;
        [DataMemberAttribute]
        public int check;

        public static List<Rol> GetDataListRolCheck(string aIdGroup)
        {

            try
            {
                DataTable dt = CMD_WEB.DAL.SrvDB.ExecuteQuery("exec SP_CMD_SELROLCHECK'" + aIdGroup + "'");
                if (dt.Rows.Count == 0)
                {
                    return null;
                }
                else
                {
                    List<Rol> list = dt.AsEnumerable().Select(
                        row => new Rol
                        {
                            idRol = String.IsNullOrEmpty(row.Field<Int64>(3).ToString())
                            ? ""
                            : row.Field<Int64>(3).ToString(),
                            dscRol = String.IsNullOrEmpty(row.Field<string>(4))
                            ? ""
                            : row.Field<string>(4),
                            check =row.Field<int>(5),


                        }
                        ).ToList();
                    return list;
                }
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        public static string AddGroup(string parametrosRol, Group group)
        {
            string mensaje = "";
            DataSet result = new DataSet();
            SqlCommand sp = new SqlCommand("SP_CMD_INSERT_REl_ROLGROUP");
            sp.CommandType = CommandType.StoredProcedure;
            Dictionary<string,string> values = JsonConvert.DeserializeObject<Dictionary<string, string>>(parametrosRol);
            try
            {
                foreach (KeyValuePair<string,string> d in values)
                {
                    sp = new SqlCommand("SP_CMD_INSERT_REl_ROLGROUP");
                    sp.CommandType = CommandType.StoredProcedure;
                    sp.Parameters.AddWithValue("ID_SECGROUP", group.idGroup.ToString());
                    sp.Parameters.AddWithValue("UNID_ROL", d.Key);
                    sp.Parameters.AddWithValue("bandera", d.Value);
                    result = SrvDB.ExecuteCommand(sp);
                    if (result.Tables.Count == 0)
                        mensaje = "Se ha guardado los datos";
                    if (result.Tables.Count == 1)
                        mensaje = "Error al guardar datos";
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }
            string sumMensaje="";
             sumMensaje+=mensaje;
             return sumMensaje;
            
        }
        public static CMD.BAL.MISC.DeleteStateMsg DeleteRol(string[] aIdRol)
        {
            CMD.BAL.MISC.DeleteStateMsg delete;
            //convirto
            string join;
            if (aIdRol == null)
            {
                join = "";
            }
            else
            {
                join = String.Join(", ", aIdRol);

            }
            //ejecuto el store
            try 
        	{
                int dt = CMD_WEB.DAL.SrvDB.ExecuteNonQuery("exec SP_CMD_DELETEROL'" + join + "'");
                delete = new MISC.DeleteStateMsg("registros eliminados " + dt.ToString(),true);
                return delete;
	        }
	        catch (Exception ex)
	        {
               delete = new MISC.DeleteStateMsg("error al eliminar" + ex, false);
		    return delete;
	        }
        }
        public static CMD.BAL.AUTH.Rol GetDataRol(string aIdRol) 
        {
            //string join;
            //if (aIdRol == null)
            //{
            //    join = "";
            //}
            //else
            //{
            //    join = String.Join(", ", aIdRol);

            //}
            //try
            //{
            //    DataTable dt = CMD_WEB.DAL.SrvDB.ExecuteQuery("exec SP_CMD_GETROL'" + join + "'");
            //    if (dt.Rows.Count == 0)
            //    {
            //        return null;
            //    }
            //    else
            //    {
            //        List<Rol> list = dt.AsEnumerable().Select(
            //            row => new Rol
            //            {
            //                idRol = String.IsNullOrEmpty(row.Field<string>(0))
            //                ? ""
            //                : row.Field<string>(0),
            //                prio = row.Field<int?>(2).GetValueOrDefault(),
            //                rol = String.IsNullOrEmpty(row.Field<string>(1))
            //                ? ""
            //                : row.Field<string>(1),
            //            }
            //            ).ToList();
            //        return list;
            //    }
            //}
            //catch (Exception ex)
            //{

            //    throw ex;
            //}
            try
            {
                Rol rol;
                DataTable dt = CMD_WEB.DAL.SrvDB.ExecuteQuery("exec SP_CMD_GETROL'" + aIdRol + "'");
                if (dt.Rows.Count == 0)
                {
                    return null;
                }
                else if (aIdRol == "")
                {
                    return null;
                }
                else
                {
                    //int a = int.Parse(dt.Rows[0]["ID_SECGROUP"].ToString());
                    //Convert.ToInt32(dt.Rows[0]["ID_SECGROUP"].ToString())
                    //int.Parse(dt.Rows[0]["ID_SECGROUP"].ToString())
                    rol = new Rol(Convert.ToString(dt.Rows[0]["UNID_ROL"]), Convert.ToInt32(dt.Rows[0]["PRIORIDAD"]), Convert.ToString(dt.Rows[0]["DSC_ROL"]));
                }
                return rol;
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
        public static CMD.BAL.MISC.UpsertStateMsg UpsertRol(Rol data)   
        {
            Rol inser;
            CMD.BAL.MISC.UpsertStateMsg insertRol;
            try
            {
                int dt = CMD_WEB.DAL.SrvDB.ExecuteNonQuery("exec SP_CMD_UPSERTROL'" + data.idRol + "','" + data.prio.ToString() + "','" + data.rol + "'");
                inser = new Rol(data.idRol,data.prio,data.rol);
                insertRol = new MISC.UpsertStateMsg("registros guardados " + dt.ToString(), true);
                return insertRol;
            }
            catch (Exception ex)    
            {
                insertRol = new MISC.UpsertStateMsg("error al guardar " + ex, false);
                return insertRol;
            }
        }
        public static List<Rol> GetDataListRol(string[] aIdRol)
        {
            string join;
            if (aIdRol == null)
            {
                join = "";
            }
            else
            {
                join = String.Join(", ", aIdRol);

            }
            try
            {
                DataTable dt = CMD_WEB.DAL.SrvDB.ExecuteQuery("exec SP_CMD_GETROL'" + join + "'");
                if (dt.Rows.Count == 0)
                {
                    return null;
                }
                else
                {
                    List<Rol> list = dt.AsEnumerable().Select(
                        row => new CMD.BAL.AUTH.Rol
                        {
                            idRol = String.IsNullOrEmpty(row.Field<string>(0))
                            ? ""
                            : row.Field<string>(0),
                            prio = row.Field<int?>(2).GetValueOrDefault(),
                            rol = String.IsNullOrEmpty(row.Field<string>(1))
                            ? ""
                            : row.Field<string>(1),
                        }
                        ).ToList();
                    return list;
                }
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
        public Rol()
        {
            //idRol = "";
            //prio = 0;
            //rol = "";
            check = 0;
        }
        public Rol(string idRol, int prio, string rol) 
        {
            this.idRol =idRol;
            this.prio = prio;
            this.rol = rol;
            this.check = 0;
        }
    }
}
